package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.PartyStudyResource;
import com.ruoyi.system.service.IPartyStudyResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 党建学习资源Controller
 */
@RestController
@RequestMapping("/api/party-study")
public class PartyStudyController extends BaseController {

    @Autowired
    private IPartyStudyResourceService partyStudyResourceService;

    /**
     * 查询学习资源列表
     */
    @PreAuthorize("@ss.hasPermi('system:partyStudy:list')")
    @GetMapping("/list")
    public TableDataInfo list(PartyStudyResource resource) {
        startPage();
        List<PartyStudyResource> list = partyStudyResourceService.selectPartyStudyResourceList(resource);
        return getDataTable(list);
    }

    /**
     * 小程序端公开列表（仅上线数据）
     */
    @Anonymous
    @GetMapping("/public/list")
    public AjaxResult publicList(PartyStudyResource resource) {
        resource.setStatus("0");
        List<PartyStudyResource> list = partyStudyResourceService.selectPartyStudyResourceList(resource);
        return success(list);
    }

    /**
     * 导出学习资源
     */
    @PreAuthorize("@ss.hasPermi('system:partyStudy:export')")
    @Log(title = "党建学习资源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PartyStudyResource resource) {
        List<PartyStudyResource> list = partyStudyResourceService.selectPartyStudyResourceList(resource);
        ExcelUtil<PartyStudyResource> util = new ExcelUtil<>(PartyStudyResource.class);
        util.exportExcel(response, list, "党建学习资源数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:partyStudy:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(partyStudyResourceService.selectPartyStudyResourceById(id));
    }

    /**
     * 新增学习资源
     */
    @PreAuthorize("@ss.hasPermi('system:partyStudy:add')")
    @Log(title = "党建学习资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PartyStudyResource resource) {
        return toAjax(partyStudyResourceService.insertPartyStudyResource(resource));
    }

    /**
     * 修改学习资源
     */
    @PreAuthorize("@ss.hasPermi('system:partyStudy:edit')")
    @Log(title = "党建学习资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PartyStudyResource resource) {
        return toAjax(partyStudyResourceService.updatePartyStudyResource(resource));
    }

    /**
     * 删除学习资源
     */
    @PreAuthorize("@ss.hasPermi('system:partyStudy:remove')")
    @Log(title = "党建学习资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(partyStudyResourceService.deletePartyStudyResourceByIds(ids));
    }
}

